package com.youmans.educms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youmans.commonutils.R;
import com.youmans.educms.entity.CrmBanner;
import com.youmans.educms.service.CrmBannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author Youmans
 * @since 2022-05-15
 */
@RestController
@Api(tags = "banner后台控制器")
@RequestMapping("/educms/bannerAdmin")
public class BannerAdminController {
    @Autowired
    private CrmBannerService crmBannerService;

    @ApiOperation("分页查询")
    @GetMapping("pageBanner/{pageNum}/{pageSize}")
    public R getBanners(
            @ApiParam("分页查询的页码数")@PathVariable("pageNum") Integer pageNum,
            @ApiParam("分页查询的记录数")@PathVariable("pageSize") Integer pageSize
            //@ApiParam("查询条件") EduTeacher eduTeacher
    ){
        //        分页带条件查询
        Page<CrmBanner> pageBanner = new Page<>(pageNum, pageSize);
//        通过服务层
        crmBannerService.page(pageBanner,null);
        long total = pageBanner.getTotal();//总记录数
        List<CrmBanner> records = pageBanner.getRecords();//数据list集合
//        封装result对象并返回
        LinkedHashMap map = new LinkedHashMap() ;
        map.put("total",total) ;
        map.put("items",records) ;
        return R.ok().data(map) ;
//        return R.ok().data("total",total).data("rows",records);
    }
    @ApiOperation("添加")
    @PostMapping("addBanner")
    public R addbanner(@RequestBody CrmBanner CrmBanner){
        crmBannerService.save(CrmBanner);
        return R.ok() ;
    }
    @ApiOperation("修改")
    @PostMapping("updateBanner")
    public R updatebanner(@RequestBody CrmBanner crmBanner){
        crmBannerService.updateById(crmBanner);
        return R.ok() ;
    }
    @ApiOperation("根据id查询")
    @GetMapping("getBannerInfo/{bannerId}")
    public R getbannerInfo(@PathVariable String bannerId){
        CrmBanner CrmBanner = crmBannerService.getById(bannerId);
        return R.ok().data("banner",CrmBanner) ;
    }
    @ApiOperation("删除")
    @DeleteMapping("{bannerId}")
    public R deletebanner(@PathVariable String bannerId){
        boolean flag = crmBannerService.removeById(bannerId);
        return flag?R.ok():R.error();
    }

}

